import * as api from "../api/api.js";
import {cleanUserInfo} from "./utils.js";


export function request({
	url = "",
	data = {},
	method = "POST",
	dataType = "json",
	showLogged = true, // 未登录时是否执行默认操作
	successCb = null, // 成功回调
	failCb = null, // 失败回调
} = {}) {
	let completeUrl = ''
	if(url.indexOf('https://') !== -1) {
		completeUrl = url
	} else {
		completeUrl = api.BASE_URL + url
	}
	// console.log('请求url', completeUrl)
	let token = uni.getStorageSync("token")
	uni.request({
		//请求路径
		url: completeUrl,
		// 参数
		data,
		// 请求方式
		method,
		// 超时时间
		timeout: 30000,
		// 一般直接写dataType:‘json’，
		dataType,
		// 设置请求头 
		header: {
			// Authorization:`Bearer ${token}`
			// token:'QInBl7GPflWAaoCdnb4ZhyEWYVBJ3nRN2zw4kgSkUgsGDUEW87eBCFjlAulgREt4lYqzyPGxYdkM5HoUGvHfmEhKms0h0bISLkBf3NLKcnJ3790gSiDWdpbisT5TyJmi'
			Authorization:'jwt eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsInR5cGUiOiJqd3QifQ.eyJpcCI6IjE0LjIzLjUyLjE5NSIsIndlYiI6eyJpZCI6MjIsImFjY291bnQiOiJcdTU4MDIiLCJwYXNzd29yZCI6IjEyMzQ1NiIsInBob25lX251bWJlciI6IjE4OTI1MDYzNDgwIiwicGhvbmVfYWRkcmVzcyI6IiIsImNyZWF0ZV90aW1lIjoiMjAyMy0xMC0xOCAyMjo1OTo1OCIsInVwZGF0ZV90aW1lIjoiMjAyNC0wNC0xMCAyMzoyNjoyOSIsImFjdGl2YXRpb25fY29kZSI6IjQxIiwiYWN0aXZhdGlvbl9jb2RlX2lkIjo0MiwiZGF0YV9hdXRob3IiOnsiU1MuU0hGIjoxNzAwNzkxMjc2LCJSQi5TSEYiOjE2OTU0ODYwMTEsIkFVLlNIRiI6MTY5NTQ4NTk1MiwiVVIuWkNFIjoxNjk1NDg2MDAwLCJJLkRDRSI6MTY5NzQ0MTg2OSwiU0MuIElORSAiOjE2OTg0MjI0MDAsIkFPLlNIRiI6MTY5NzgwODE2MX0sImlwX2FkZHJlc3MiOlsiXHU1ZTdmXHU0ZTFjOlx1NWU3Zlx1NWRkZSIsIlx1NWU3Zlx1NGUxYzoiLCJcdTRlMGFcdTZkNzc6XHU0ZTBhXHU2ZDc3IiwiXHU1ZTdmXHU0ZTFjOlx1NmRmMVx1NTczMyJdLCJ2aXAiOmZhbHNlLCJzdGF0dXMiOnRydWUsImV4cGVyaWVuY2VfdGltZSI6IjIwMjYtMTItMzEgMTI6MDA6MDAiLCJzdmlwX3RpbWUiOm51bGwsImlwX3RpbWUiOjE3MTI4MDI2ODZ9LCJleHAiOjE3MTMwMTg2ODZ9.d0XaNNT5F0Yf4DYswOLE196ZFKPigW8eElZWQ7aiP3M'
		},
		success: (res) => {
			// console.log('封装函数返回数据', res)
			//  可以根据自己需求  根据 code 进行拦截
			if (res.statusCode === 200) {
				// res.data.code === 200
				if(res.data.status || res.data.message == 'success!') {
					// 需要成功回调时才调用
					successCb && successCb(res.data);
				} else if(res.data.code === 401) {
					showLogged && uni.showToast({
						title: "未登录",
						icon: "none",
						duration: 2000,
					});
					
					cleanUserInfo();
					uni.navigateTo({
						url: '/pages/logon/logon/logon'
					});
				} else {
					failCb && failCb(res);
				}
			} else {
				// 请求成功  业务失败
				uni.showToast({
					title: "请求失败",
					icon: "none",
					duration: 2000,
				});
			}
		},
		// 请求失败
		fail(err) {
			uni.showToast({
				title: "请求失败",
				icon: "none",
				duration: 2000,
			});
			//   需要失败回调时才调用
			failCb && failCb(err);
		},
	});
}
export function uploadFile(url, filePath,formData) {
	return new Promise((resolve, reject) => {
		let header = {
			// 'tenant': 'MDAwMA=='
		}
		let token = uni.getStorageSync('token')
		if (token) {
			header['Authorization'] = 'Bearer ' + token
		}
		uni.uploadFile({
			url: api.BASE_URL+url,
			filePath: filePath,
			name: 'file',
			header: header, // 设置请求的 header
			formData: formData || {}, // HTTP 请求中其他额外的 form data
			success: function(res) {
				if (res.statusCode == 200) {
					var json = res.data?JSON.parse(res.data):{};
					if (json.code == 200) {
						uni.hideLoading();
						resolve(json);
					} else {
						// uni.showToast({
						// 	title:res.data.msg,
						// 	icon:'none',
						// 	duration:1000
						// })
						reject(json);
					}
 
				} else if (res.statusCode == 401) {
					// 登录超时
					// refreshToken().then(res1=>{
					// 	uploadFile(url, filePath,formData).then(res2=>{
					// 		resolve(res2);
					// 	}).catch(e=>{
					// 		reject(e);
					// 	});
					// }).catch(e=>{
					// 	reject(res);
					// });
					uni.showToast({
						title:res.data.msg,
						icon:'none',
						duration:1000
					})
					reject(res);
				} else {
					uni.showToast({
						title:res.data.msg,
						icon:'none',
						duration:1000
					})
					reject(res.data);
				}
			},
			fail: function(err) {
				if(err.errMsg!=null&&err.errMsg.indexOf("request:fail")>=0){
					uni.showToast({
						title:err.msg,
						icon:'none',
						duration:1000
					})
					reject(null)
				}else{
					uni.showToast({
						title:err.msg,
						icon:'none',
						duration:1000
					})
					reject(err)
				}
			}
		})
	});
}
// 导出 request
export default request;